package app.android.com.wuandroidapp.test.bservice;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.os.EnvironmentCompat;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

import app.android.com.wuandroidapp.AppConstants;

/**
 * Created by admin on 2017/4/26.
 */

public class PhoneListenService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
    }

    private class MyPhoneStateListener extends PhoneStateListener {
        private String number;
        private MediaRecorder recorder;
        private File mFile;

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            try {
                switch (state) {
                    case TelephonyManager.CALL_STATE_RINGING://来电状态
                        number = incomingNumber;
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK://电话接通
                        mFile = new File(AppConstants.WUAPPPHONE+"/"+System.currentTimeMillis() + number + ".3gp");
                        //判断文件是否存在，存在就删除
                        if (mFile.exists()){
                            mFile.delete();
                        }
                        try {
                            //创建文件
                            mFile.createNewFile();
                            //给一个吐司提示，显示创建成功
                            Toast.makeText(getApplicationContext(), "文件创建成功", Toast.LENGTH_SHORT).show();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        recorder = new MediaRecorder();
                        //声音来源麦克风
                        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        //    设置输出格式:
                        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        //   设置音频编码
                        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        //   设置输出文件
                        recorder.setOutputFile(mFile.getAbsolutePath());
                        //    让MediaRecorder对象处于就绪状态
                        recorder.prepare();
                        //开始录音
                        recorder.start();
                        break;
                    case TelephonyManager.CALL_STATE_IDLE://电话空闲
                        if(recorder!=null){
                            recorder.stop();//停止录音
                            recorder.release();//释放资源
                            recorder=null;
                        }
                        break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    }
}
